﻿/****************************** Module Header ******************************\
 * Module Name:  SMSHelper.cs
 * Module Version:  1.0.0 (2012-2-15)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to send email by Net MailMessage dll. It supplies 
 * useful method to send a email by SMTP server.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Net;

namespace UCF.Core
{
    /// <summary>
    /// 手机短信帮助类
    /// </summary>
    public class SMSHelper
    {
        // 接口地址
        private string apiUri;

        // 接收手机变量名称
        private string toVar;

        // 内容变量名称
        private string bodyVar;

        /// <summary>
        /// 构造方法
        /// </summary>
        public SMSHelper() { }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="apiUri">接口地址中包含变量模板定义：{0}=登录账户,{1}=登录密码,{2}=短信内容,{3}=接收手机号码,{4}=发送时间</param>
        /// <param name="args">{0}=登录账户,{1}=登录密码,{2}=短信内容,{3}=接收手机号码,{4}=发送时间</param>
        public SMSHelper(string apiUri, params string[] args)
        {
            this.apiUri = string.Format(apiUri, args);
        }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="apiUri"></param>
        /// <param name="toVar"></param>
        /// <param name="bodyVar"></param>
        /// <param name="args"></param>
        public SMSHelper(string apiUri, string toVar, string bodyVar,params string[] args)
        {
            this.toVar = toVar;
            this.bodyVar = bodyVar;
            this.apiUri = string.Format(apiUri, args);
        }
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="toMobile"></param>
        /// <param name="bodyValue"></param>
        /// <returns></returns>
        public string Send(string toMobile,string bodyValue)
        {
            if (this.apiUri == null || this.apiUri.Length == 0) return "";
            string sApi = this.apiUri;
            if(this.bodyVar!=null && this.bodyVar.Length>0) sApi += (sApi.IndexOf('?') >= 0 ? "&" : "?") + this.bodyVar + "=" + bodyValue;
            if(this.toVar!=null && this.toVar.Length>0) sApi += "&" + this.toVar + "=" + toMobile;
            return (new WebClient()).DownloadString(sApi);
        }
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <returns></returns>
        public string Sender()
        {
            return (new WebClient()).DownloadString(this.apiUri);
        }
    }
}
